<template>
  <div>
    <left-drawer ref="drawer"></left-drawer>
    <home-top ref="homeTop" @showDrawer="showDrawer" @onTabChange="onTabChange" v-show="top"></home-top>
    <slot name="content"></slot>
  </div>
</template>

<script>
import LeftDrawer from './LeftDrawer';
import HomeTop from './HomeTop';
import { mapGetters } from 'vuex';
import { GET_CURRENT_LOCATION } from '@/store/actions.types';
import { SET_START_CITY_OBJ } from '@/store/mutations.types';
export default {
  name: 'home-frame',
  data() {
    return {
      drawer: false,
      top: true
    };
  },
  components: {
    [LeftDrawer.name]: LeftDrawer,
    [HomeTop.name]: HomeTop
  },
  created() {
    console.log('当前GPS状态', this.gpsStatus);
    if (this.gpsStatus != '2') {
      this.$store
        .dispatch(GET_CURRENT_LOCATION)
        .then(() => {
          let city = {
            start_route: this.getCurrentLocation.provinceName,
            type: '1'
          };
          this.$store.commit(SET_START_CITY_OBJ, city);
        })
        .catch(reason => {
          console.error('当前浏览器没有位置权限', reason.message);
        });
    }
  },
  methods: {
    showDrawer() {
      this.$refs.drawer.showDrawer();
    },
    onTabChange(tabName) {
      this.$store.commit('resetTaxi');
      this.$store.commit('resetIncity');
      this.$store.commit('resetRoute');
      switch (tabName) {
        case '城际出行': // 默认首页
          if (this.$route.name !== 'Home') {
            this.$router.replace({
              name: 'Home'
            });
          }
          break;
        case '市内用车': // 市内用车
          if (this.$route.name !== 'homeIncity') {
            this.$router.replace({
              name: 'homeIncity'
            });
          }
          break;
        case '出租车': // 出租车
          if (this.$route.name !== 'homeTaxi') {
            this.$router.replace({
              name: 'homeTaxi'
            });
          }
          break;
        case '顺风车':
          if (this.$route.name !== 'homeHitchhike') {
            this.$router.replace({
              name: 'homeHitchhike'
            });
          }
          break;
        default:
          // 首页
          this.$router.replace({
            name: 'Home'
          });
      }
    },
    hideTop() {
      this.top = false;
    },
    showTop() {
      this.top = true;
    }
  },
  computed: {
    ...mapGetters(['gpsStatus', 'getCurrentLocation'])
  }
};
</script>
